
CurrentPage.page = datagrid_SalesInput(CurrentPage.id);
mainPage.add(CurrentPage.page);
mainPage.setActiveTab(CurrentPage.id);

function datagrid_SalesInput(mod_id)
{
	var Field = 
	[
		'BARCODE','SIZE','COLOR','BARDESCR','SLSQTY','DISCARMOUNT','SLSTOAMT','SLSPRICE','ARTCEXT'
	];
	
	 dataSource_SalesInput = new WebApp.DataStore
	({
        fields: Field
    });
	
	
	
	var Grid_SalesInput = new Ext.grid.EditorGridPanel
    ({
        //xtype: 'editorgrid',
        title: '',
        store: dataSource_SalesInput,
        autoScroll: true,
        border:false,  
		stripeRows: true,
        anchor: '100% 100%',
        trackMouseOver:false,
        loadMask: true,
        selModel: new Ext.grid.RowSelectionModel
        ({
            singleSelect: true,
            listeners:
            {
                rowselect: function(sm, row, rec)
                {
					rowSelected_SalesInput = dataSource_SalesInput.getAt(row);
					currentRowSelected_SalesInput.row = row;
					currentRowSelected_SalesInput.data = rowSelected_SalesInput;
                }
            }
        }),
        colModel: new Ext.grid.ColumnModel
        (
            [
				new Ext.grid.RowNumberer(),
				{
					id: 'ColBarcode_SalesInput',
					header: 'Barcode',
					dataIndex: 'BARCODE',
					sortable: true,
					width: 100,
					editor: new Ext.form.TextField
					(
						{
							id: 'fieldcolBarcode_SalesInput',
							allowBlank: false,						
							enableKeyEvents: true,
							listeners:
							{
								'specialkey': function(field,e)
								{
									if (e.getKey() == e.ENTER)
									{
										var flag = false;
										if(this.getValue().length < 14)
										{
											flag = true;
											alert('kurang 14 buka windows form');
										}
										if(this.getValue().length == 14)
										{
											//alert('masukan data barang ke grid');
											var p = new mRecordSalesInpuit
											({
												'BARDESCR':'',
												'ARTCEXT':'',
												'SIZE':'',
												'COLOR':'',
												'SLSPRICE':'',
												'DISCARMOUNT':'',
												'BARCODE':''
											});
											var query = this.getValue() + "#" + Ext.get('date_SalesInput').getValue();
											BindingDataBarang_SalesInput(query,p,flag,dataSource_SalesInput,currentRowSelected_SalesInput.row);
										}
									}
								}
							}
						}
					)
				},                   
				{
					id: 'colItemID_SalesInput',
					header: 'Items ID', 
					dataIndex: 'ARTCEXT',
					width: 220,
					sortable: true
				},
				{
					id: 'colSize_SalesInput',
					header: 'Size', 
					dataIndex: 'SIZE',
					width: 100,
					sortable: true
				},
				{
					id: 'colColor_SalesInput',
					header: 'Color', 
					dataIndex: 'COLOR',
					width: 100,
					sortable: true			
				},
				{
					id: 'colDescript_SalesInput',
					header: 'Item Description', 
					dataIndex: 'BARDESCR',
					width: 145,
					sortable: true
				},
				{
					id: 'colQty_SalesInput',
					header: 'Qty', 
					dataIndex: 'SLSQTY',
					width: 100,
					sortable: true,
					editor: new Ext.form.TextField
					({
						id:'fieldcolQty_SalesInput',
						allowBlank:false,
						enableKeyEvents: true,
						listeners:
						{
							'specialkey': function(field,e)
							{
								if (e.getKey() == e.ENTER)
								{
									var Total, disc, qty, harga = 0;
									disc = rowSelected_SalesInput.data.DISCARMOUNT;
									qty = this.getValue();
									harga = rowSelected_SalesInput.data.SLSPRICE;
									Total = qty * (harga * (disc / 100));
									rowSelected_SalesInput.data.SLSTOAMT = Total;
									totalDiscAmount = Total;
									SaveSalesInput(true);
								}
							}
						}
					})
				},
				{
					id: 'colPrice_SalesInput',
					header: 'Price', 
					dataIndex: 'SLSPRICE',
					width: 100,
					sortable: true
				},
				{
					id: 'colDisc_SalesInput',
					header: 'Disc', 
					// dataIndex: 'PENERBIT',
					width: 100,
					sortable: true		
				},
				{
					id: 'colTotal_SalesInput',
					header: 'Total', 
					dataIndex: 'SLSTOAMT',
					width: 100,
					sortable: true
				}
            ]
        ), 	
        tbar: {
            xtype: 'toolbar',
            id: 'toolbar_viSetKoleksiBuku',
            items: [
            {
                xtype: 'button',
                text: 'Add Item',
                iconCls: '',
                tooltip: 'Add Item',
                id: 'btnAddItem_SalesInput',
                handler: function() 
                {	
					var p = new mRecordSalesAddRow
					(
						{
							BARCODE : ''
						}
					);
					dataSource_SalesInput.insert(dataSource_SalesInput.getCount(),p);
                }                        
            },
			{xtype:'tbfill'},
			{
				xtype: 'button',
                text: 'New Transaction',
                iconCls: '',
                tooltip: 'New Transaction',
                id: 'btnNewTransact_SalesInput',
				handler: function()
				{
					
				}
			},
			'-',
            {
                xtype: 'button',
                text: 'Confirm',
                iconCls: '',
                tooltip: 'Confirm Data',
                id: 'btnConfirm_SalesInput',
				handler: function ()
				{
					SaveSalesInput(false);
				}
            }
            ]
        },
        viewConfig: {
            forceFit: false
        }    
    }   
    );
	
	var mRecordSalesAddRow = Ext.data.Record.create
	(
		[
			'BARCODE'
		]
	);
	
	var FrmTabs_SalesInput = new Ext.Panel
    (
		{
			xtype: 'panel',
			title: 'Sales Input',
			layout: 'form',
			closable: true,
			iconCls: '',
			id: mod_id,
			shadhow: true,
			margins: '0 5 5 0',
			anchor: '100%',
			items:[Grid_SalesInput],
		    tbar:
			[
				'Sales date' + ' : ', ' ',
				{
				    xtype: 'datefield',
				    id: 'date_SalesInput',
					format: 'd M Y',
				    width: 100,
					listeners: 
					{
						change: function(el,type) 
						{
							selectTgl = Ext.get('date_SalesInput').getValue();
							LoadNoNota(Ext.get('date_SalesInput').getValue());
						}                         
					}
				}, ' ', '-',
				'Nota no' + ' : ', ' ',
					ComboNota(),' ', '-',
				'Id sales' + ' : ', ' ',
				{
				    xtype: 'textfield',
				    fieldLabel: 'Id sales : ',
				    id: 'txtIdSales_SalesInput',
				    anchor: '95%',	    
				    listeners:
					{
						'specialkey': function(field,e)
						{
							if (e.getKey() == e.ENTER)
							{
								alert('id Sales');
							}
						}
					}
				},{xtype: 'tbfill'},mLabelCount('lblKeteranganToko')
			],
			listeners:
			{ 'afterrender': function() {
				alert('untuk mengambil data toko');
			}
			}
		}
    )
	
	//BindingData_SalesInput('');
	
	return FrmTabs_SalesInput;
}

function mLabelCount(nmLbl)
{
	var lblCount = new Ext.form.Label
	(
		{
			id: nmLbl,
			text: 'Nama Toko'
		}
	)
	return lblCount;
}

function validationSalesInput()
{
	var x = 1;
	
	if(dataSource_SalesInput.getCount == 0)
	{
		msgWarn_SalesInput('Tidak ada data sales yang diinput','Sales Input');
		x = 0;
		alert('Validation Column Yang belum diisi');
	}
	
	return x;
}

function msgWarn_SalesInput(msg,title)
{   
    Ext.MessageBox.show
    (
		{
			title: title,
			msg:msg,
			buttons: Ext.MessageBox.OK,
			icon: Ext.MessageBox.WARNING,
			width :250
		}
    )
}

function DataRow_SalesInput()
{
	rowSelected_SalesInput.data.BARDESCR = 'asdaw';
}

function getDataBarang()
{
	var x = '';
	
	for (var i = 0; i < dataSource_SalesInput.getCount(); i++)
	{
		var y = '';
		var z = '@@##$$@@';
		y+= 'BARCODE=' + dataSource_SalesInput.data.items[i].data.BARCODE
		y += z + 'SIZE=' + dataSource_SalesInput.data.items[i].data.SIZE
		y += z + 'COLOR=' + dataSource_SalesInput.data.items[i].data.COLOR
		y += z + 'SLSPRICE=' + dataSource_SalesInput.data.items[i].data.SLSPRICE
		y += z + 'DISCARMOUNT=' + dataSource_SalesInput.data.items[i].data.DISCARMOUNT
		y += z + 'BARDESCR=' + dataSource_SalesInput.data.items[i].data.BARDESCR
		y += z + 'SLSTOAMT=' + dataSource_SalesInput.data.items[i].data.SLSTOAMT
		y += z + 'ARTCEXT=' + dataSource_SalesInput.data.items[i].data.ARTCEXT
		y += z + 'SLSQTY=' + Ext.getCmp('fieldcolQty_SalesInput').getValue()
		if (i === (dataSource_SalesInput.getCount() - 1)) 
		{
            x += y
        }
        else {
            x += y + '##[[]]##'
        }
	}
	return x
}

function ComboNota()
{
	var Field = ['SLSNOTA','SLSNO'];
    dsNota = new WebApp.DataStore({fields: Field});
    
	LoadNoNota('');
    var cboNoNota = new Ext.form.ComboBox
    (
        {
            flex: 1,
            fieldLabel: 'Nota No',
			valueField: 'SLSNOTA',
            displayField: 'SLSNOTA',
			store: dsNota,
            width: 100,
            mode: 'local',
            typeAhead: true,
            triggerAction: 'all',                        
            name: 'cboNoNota',
            lazyRender: true,
            id: 'cboNoNota',
            listeners:
			{
				'select': function (a,b,c)
				{
					var selectNota = b.data.SLSNOTA;
					var selectNo = b.data.SLSNO;
					var query = selectNota + "**" + selectNo + "**" + selectTgl;
					Ext.get('txtIdSales_SalesInput').dom.value = selectNo;
					BindingData_SalesInput(query);
				}
			}
        }
    )    
    return cboNoNota;
}





